package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.dao.GoodsMapper;
import com.itheima.goods.pojo.Brand;
import com.itheima.goods.pojo.Category;
import com.itheima.goods.pojo.Goods;
import com.itheima.service.GoodsService;
import entity.PageResult;
import entity.PageUtil;
import entity.Result;
import entity.StatusCode;
import org.springframework.stereotype.Service;

@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements GoodsService {

    //分页
    @Override
    public Result<Object> findPage(Integer page, Integer size) {
        //TODO:1.创建Page分页对象
        Page<Goods> pageInfo = new Page<>();
        pageInfo.setCurrent(page);
        pageInfo.setSize(size);
        //TODO:2.使用baseMapper执行分页查询
        baseMapper.selectPage(pageInfo, null);
        //TODO:3.创建分页转换工具类
        PageUtil<Goods> pageUtil = new PageUtil<>();
        PageResult<Goods> pageResult = pageUtil.transform(pageInfo);
        //TODO:4创建响应结果对象,判断查询结果并将数据响应给前端
        Result<Object> result = new Result<>();
        if (pageResult==null){
            //查询失败,响应错误信息给前端
            result.setCode(StatusCode.ERROR);
            result.setFlag(false);
            result.setData(null);
            result.setMessage("查询失败");
            return result;
        }
        //查询成功,响应信息给前端
        result.setCode(StatusCode.OK);
        result.setFlag(true);
        result.setData(pageResult);
        result.setMessage("查询成功");
        return result;
    }

    //修改前回显
    @Override
    public Result<Object> findById(Integer id) {
        Goods goods = baseMapper.selectById(id);
        if (goods==null){
            return new Result<>(false,StatusCode.ERROR,"查询数据不存在!",null);
        }
        return new Result<>(true,StatusCode.OK,"查询成功!",goods);
    }

    //修改
    @Override
    public Result<Object> updateGoodsById(Goods goods) {
        int i = baseMapper.updateById(goods);
        if (i<0){
            //修改失败
            return new Result<>(false,StatusCode.ERROR,"修改数据失败!",null);
        }
        return new Result<>(true,StatusCode.OK,"修改成功!",null);
    }

    //删除
    @Override
    public Result<Object> deleteGoodsById(Integer id) {
        int i = baseMapper.deleteById(id);
        if (i<0){
            //删除失败
            return new Result<>(false,StatusCode.ERROR,"删除失败!",null);
        }
        return new Result<>(true,StatusCode.OK,"删除成功!",null);
    }

    //添加
    @Override
    public Result<Object> addGoods(Goods goods) {
        LambdaQueryWrapper<Goods> brandLambdaQueryWrapper = Wrappers.lambdaQuery(Goods.class);
        brandLambdaQueryWrapper.eq(Goods::getName,goods.getName());
        //先查询要添加的数据是否已经存在
        Goods oneGoods = baseMapper.selectOne(brandLambdaQueryWrapper);
        if(oneGoods!=null){
            //要添加的数据已存在
            return new Result<>(false,StatusCode.ERROR,"要添加的品牌已存在!",null);
        }
        int i = baseMapper.insert(goods);
        if (i<0){
            //添加失败
            return new Result<>(false,StatusCode.ERROR,"添加失败!",null);
        }
        return new Result<>(true,StatusCode.OK,"添加成功!",null);
    }
}
